<?php

namespace App\Admin\Controllers;

use App\Models\DistributorLevel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DistributorLevelController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '分销等级';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DistributorLevel());
        
        $grid->column('name', __('Name'));
        $grid->column('level', __('Level'));
        $grid->column('one_level_bonus', __('One level bonus'))->display(function ($data) {
        	return $data.'%';
        })->sortable();
        $grid->column('two_level_bonus', __('Two level bonus'))->display(function ($data) {
        	return $data.'%';
        })->sortable();
        $grid->column('three_level_bonus', __('Three level bonus'))->display(function ($data) {
        	return $data.'%';
        })->sortable();
        $grid->column('created_at', __('Created at'))->sortable();
		
        $grid->filter(function ($filter) {
        	$filter->disableIdFilter();
        	$filter->like('name', __('Name'));
        	$filter->equal('level', __('Level'))->select(DistributorLevel::all()->pluck('name', 'id'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DistributorLevel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('level', __('Level'));
        $show->field('one_level_bonus', __('One level bonus'));
        $show->field('two_level_bonus', __('Two level bonus'));
        $show->field('three_level_bonus', __('Three level bonus'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DistributorLevel());

        $form->text('name', __('Name'))->required();
        $form->hidden('level', __('Level'))
	        ->default(1)
	        ->rules('required|numeric:max:3|min:1');
        $form->rate('one_level_bonus', __('One level bonus'))->default(0);
        $form->rate('two_level_bonus', __('Two level bonus'))->default(0);
        $form->rate('three_level_bonus', __('Three level bonus'))->default(0);

        return $form;
    }
}
